﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;


namespace QQ.Util
{
    static class RTFile
    {

        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
       static public string ReadFile(string filename)
        {
            if (!File.Exists(filename))
            {
                Console.WriteLine(filename + "不存在");
                return "";
            }
            else
            {
                FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);
                String line = sr.ReadToEnd();
                sr.Close();
                fs.Close();
                return line;
            }
        }

        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="content"></param>
        /// <returns></returns>
      static  public bool WriteFile(string filename, string content)
        {
            StreamWriter sw = File.CreateText(filename);
            //开始写入
            sw.Write(content);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            return true;
        }



      /// <summary>
      /// 递归搜索某一目录下的所有指定扩展名的文件，调用实例：ListFiles(new DirectoryInfo("C:\\"), "txt", textBox1);
      /// </summary>
      /// <param name="info">目录路径</param>
      /// <param name="Ext">指定扩展名</param>
      /// <param name="obj">用于显示的文本框</param>
      public static List<string> ListFiles(FileSystemInfo info, string Ext)
      {

         
          if (!info.Exists) return null;
          List<string> list = new List<string>();

          DirectoryInfo dir = info as DirectoryInfo;
          //不是目录 
          if (dir == null) return null;
          try
          {

              FileSystemInfo[] files = dir.GetFileSystemInfos();
              for (int i = 0; i < files.Length; i++)
              {
                  FileInfo file = files[i] as FileInfo;
                  //是文件
                  if (file != null && file.Extension.ToUpper() == "." + Ext.ToUpper())
                  { 
                      list.Add(file.FullName);
                      
                  }
                  //对于子目录，进行递归调用 
                  else
                  {

                  }

              }
          }
          catch (UnauthorizedAccessException ex)
          {
          	ex.ToString();
          }

          return list;

      }


      public static void creatDir(string path)
      {


          try
          {
              // Determine whether the directory exists.
              if (Directory.Exists(path))
              {
                  Console.WriteLine("目录已经存在");
                  return;
              }

              // Try to create the directory.
              DirectoryInfo di = Directory.CreateDirectory(path);
              Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path));


          }
          catch (Exception e)
          {
              Console.WriteLine("The process failed: {0}", e.ToString());
          }
          finally { }


      }


        public static void creatFile(string path)
        {
            try
            {
               
                if (File.Exists(path))
                {
                    Console.WriteLine("文件已经存在！不用在建立了");
                    return;
                }
                else
                {
                    File.Create(path);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("发生异常了！建立文件：{0}", e.ToString());
            }
          finally { }

        }


      
    }
}
